/*
* $Id: ViewInspectWizard.java 278 2011-03-04 22:31:44Z volok-aleksej@yandex.ru $
*
* Copyright 2008-2011 by Volokitin Alexey
* All rights reserved
*
*/
package apiary.component.journal;

import apiary.Apiary;
import apiary.common.ConvertFunction;
import apiary.common.FormHelper;
import apiary.db.FieldsValueStore.FieldsValueRecord;
import apiary.db.InspectStore.InspectRecord;
import apiary.db.managers.DBManager;
import apiary.db.managers.InspectManager.InspectManagerRecord;
import apiary.settings.SettingStore;
import apiary.settings.SettingsRecord;
import apiary.ui.BackScreen;
import apiary.ui.journal.ChooseForm;
import apiary.ui.journal.ChooseForm.ChooseValue;
import apiary.ui.journal.ViewForm;
import apiary.ui.journal.WizardScreen;
import java.util.Hashtable;
import java.util.Vector;

/**
 * Wizard of view inspect
 * @author lexus
 */
public class ViewInspectWizard extends Wizard
{
    /**
     * Constructor
     */
    public ViewInspectWizard() {
        super(null);
        Init();
    }

    /**
     * Constructor
     * @param parentForm Form where it want to move when pressing Back.
     * This form used only on first form of wizard
     */
    public ViewInspectWizard(BackScreen parentForm) {
        super(parentForm);
        Init();
        setCancelForm(parentForm);
    }

    /**
     * Initialize Wizard
     */
    private void Init() {
        Apiary apiary = Apiary.getInstance();
        Integer idChoose = new Integer(1);
        firstForm = new ChooseForm(apiary.getLocaleString("ChooseFamily"), idChoose, this, true);
        FormHelper.fillFamilyList((ChooseForm) firstForm);
        Integer idChooseInspect = new Integer(2);
        ChooseForm chooseForm = new ChooseForm(apiary.getLocaleString("ChooseInspect"), idChooseInspect, this, true);
        chooseForm.setBackScreen(firstForm);
        formsNext.put(idChoose, chooseForm);
        Integer idView = new Integer(3);
        ViewForm form = new ViewForm(apiary.getLocaleString("ViewInspect"), idView, this);
        form.setBackScreen(chooseForm);
        formsNext.put(idChooseInspect, form);
    }

    /**
     * @deprecated In this wizard don't use
     */
    public void save() {
    }

    /**
     * Calls before to show next form of wizard
     * @param nextForm next form of wizard
     */
    public void next(BackScreen nextForm) {
        WizardScreen screen = (WizardScreen) nextForm;
        if(screen.getIdForm().intValue() == 2) {
            ChooseValue value = (ChooseValue) getData(new Integer(1));
            FormHelper.fillInspectList((ChooseForm) nextForm, value.id);
        } else if(screen.getIdForm().intValue() == 3) {
            ChooseValue value = (ChooseValue) getData(new Integer(2));
            setJournalData((ViewForm)nextForm, value.id);
        }
    }

    /**
     * Checks data of form on truth
     * @param dataForm data of form
     * @param idForm identificator of form
     * @return truth of data
     */
    public boolean isOk(Integer idForm, Object dataForm) {
        return true;
    }

    /**
     * Form is filled common inspect data
     * @param viewForm form
     * @param id identificator of inspect
     */
    private void setJournalData(ViewForm viewForm, int id) {
        Apiary apiary = Apiary.getInstance();
        DBManager dBManager = DBManager.getInstance();
        InspectManagerRecord inspect = dBManager.getInspect(id);
        viewForm.append(apiary.getLocaleString("DateInspect").concat(": "));
        String date = ConvertFunction.dateToString(inspect.journal.date.getTime());
        viewForm.append(date.concat("\n"));
        viewForm.append(apiary.getLocaleString("CountFrame").concat(": "));
        viewForm.append(String.valueOf(inspect.journal.frame).concat("\n"));
        viewForm.append(apiary.getLocaleString("Strength").concat(": "));
        viewForm.append(String.valueOf(inspect.journal.strength).concat("\n"));
        viewForm.append(apiary.getLocaleString("IsQueenBee").concat(": "));
        Hashtable qbState = dBManager.getQBStates();
        String isQB = (String) qbState.get(new Integer(inspect.journal.is_queen_bee));
        viewForm.append(isQB.concat("\n"));
        SettingStore settings = SettingStore.getInstance();
        if(settings.getData(SettingsRecord.GIVE_WOX).getValue() == Boolean.TRUE) {
            viewForm.append(apiary.getLocaleString("GiveWox").concat(": "));
            viewForm.append(String.valueOf(inspect.journal.wax).concat("\n"));
        }
        if(settings.getData(SettingsRecord.AMBROSIA).getValue() == Boolean.TRUE) {
            viewForm.append(apiary.getLocaleString("Ambrosia").concat(": "));
            viewForm.append(String.valueOf(inspect.journal.ambrosia).concat("\n"));
        }
        if(settings.getData(SettingsRecord.DISEASES).getValue() == Boolean.TRUE) {
            viewForm.append(apiary.getLocaleString("Diseases").concat(": "));
            Hashtable diseases = dBManager.getDiseases();
            String disease = (String) diseases.get(new Integer(inspect.journal.disease));
            viewForm.append(disease.concat("\n"));
        }
        if(settings.getData(SettingsRecord.FEEDING).getValue() == Boolean.TRUE) {
            viewForm.append(apiary.getLocaleString("Feeding").concat(": "));
            viewForm.append(String.valueOf(inspect.journal.dieting).concat("\n"));
        }
        if(settings.getData(SettingsRecord.PUMP_HONEY).getValue() == Boolean.TRUE) {
            viewForm.append(apiary.getLocaleString("PumpHoney").concat(": "));
            viewForm.append(String.valueOf(inspect.journal.evacuation).concat("\n"));
        }
        if(settings.getData(SettingsRecord.NOTE_INSPECT).getValue() == Boolean.TRUE) {
            viewForm.append(apiary.getLocaleString("Note").concat(": "));
            viewForm.append(inspect.journal.noteJournal.concat("\n"));
        }
        Vector fieldValues = dBManager.getFieldsValue(inspect.journal.idRecord);
        setUserField(viewForm, fieldValues);
        setInspectData(viewForm, inspect.inspects);
    }

    /**
     * Form is filled user data
     * @param userFields user data of family
     * @param form form for fill
     */
    private void setUserField(ViewForm form, Vector userFields) {
        for(int i = 0; i < userFields.size(); i++) {
            FieldsValueRecord fvRecord = (FieldsValueRecord) userFields.elementAt(i);
            form.append(fvRecord.nameField.concat(": "));
            form.append(fvRecord.valueField.concat("\n"));
        }
    }

    /**
     * Form is filled hive inspect data
     * @param inspects hive inspect of family
     * @param form form for fill
     */
    private void setInspectData(ViewForm form, Vector inspects) {
        Apiary apiary = Apiary.getInstance();
        for(int i = 0; i < inspects.size(); i++) {
            InspectRecord inspect = (InspectRecord) inspects.elementAt(i);
            form.append("\n");
            form.append(inspect.nameHousing.concat("\n"));
            form.append(apiary.getLocaleString("Honey").concat(": "));
            form.append(String.valueOf(inspect.honey).concat("\n"));
            form.append(apiary.getLocaleString("Brood").concat(": "));
            form.append(String.valueOf(inspect.brood).concat("\n"));
            SettingStore settings = SettingStore.getInstance();
            if(settings.getData(SettingsRecord.OPEN_BROOD).getValue() == Boolean.TRUE) {
                form.append(apiary.getLocaleString("OpenBrood").concat(": "));
                form.append(String.valueOf(inspect.openBrood).concat("\n"));
            }
            if(settings.getData(SettingsRecord.SEAL_BROOD).getValue() == Boolean.TRUE) {
                form.append(apiary.getLocaleString("SealBrood").concat(": "));
                form.append(String.valueOf(inspect.closeBrood).concat("\n"));
            }
            if(settings.getData(SettingsRecord.GOODS_HONEY).getValue() == Boolean.TRUE) {
                form.append(apiary.getLocaleString("GoodsHoney").concat(": "));
                form.append(String.valueOf(inspect.goodsHoney).concat("\n"));
            }
        }
    }
}
